package com.spring.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spring.crm.conf.CommonResult;
import com.spring.crm.entity.ContractV;
import com.spring.crm.mapper.ContractVMapper;
import javafx.application.Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * VIEW 前端控制器
 * </p>
 *
 * @author 何卓
 * @since 2022-02-17
 */
@RestController
@RequestMapping("/api/crm/contract-v")
public class ContractVController {

    private final static Logger LOGGER = LoggerFactory.getLogger(Application.class);

    @Resource
    private ContractVMapper contractVMapper;
    
    @CrossOrigin(origins = "*")
    @GetMapping("condition")
    public CommonResult selectByCondition(@RequestParam Map<String,String> map) {
        CommonResult rs;
        try {
            QueryWrapper<ContractV> queryWrapper = new QueryWrapper<>();
            if(map.containsKey("contractId")){
                queryWrapper.eq("contract_id",map.get("contractId"));
            }
            if(map.containsKey("customId")){
                queryWrapper.eq("custom_id",map.get("customId"));
            }
            if(map.containsKey("contractName")){
                queryWrapper.like("contract_name",map.get("contractName"));
            }
            if(map.containsKey("contractStatus")){
                queryWrapper.eq("contract_status",map.get("contractStatus"));
            }
            if(map.containsKey("salesId")){
                queryWrapper.eq("sales_id",map.get("salesId"));
            }
            //queryWrapper.orderByAsc("num");
            Page<ContractV> page = new Page<>(Long.valueOf(map.get("current")),Long.valueOf(map.get("size")));
            IPage<ContractV> SalesIPage = contractVMapper.selectPage(page,queryWrapper);
            Integer selectCount = Math.toIntExact(contractVMapper.selectCount(queryWrapper));
            List<ContractV> list = SalesIPage.getRecords();
            rs = CommonResult.success(list,selectCount);
        }catch(Exception ex){
            ex.printStackTrace();
            rs = CommonResult.failed(ex.toString());
        }
        return rs;
    }
}
